﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;

namespace SaleSystem.BaseData
{
    public partial class frm_ProductInfoEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;            
            SModel.State = 1;
            SModel.PInfo_CH = this.tb_PInfo_CH.Text;
            SModel.PInfo_Code = this.tb_PInfo_Code.Text;
            SModel.PInfo_Cost = ValueHandler.GetDecNumberValue( this.tb_PInfo_Cost.Text);
            SModel.PInfo_Memo = this.tb_PInfo_Memo.Text;
            SModel.PInfo_Name = this.tb_PInfo_Name.Text;
            SModel.PInfo_Number = this.tb_PInfo_Number.Text;
            SModel.PInfo_Price = ValueHandler.GetDecNumberValue( this.tb_PInfo_Price.Text);
            SModel.PInfo_PSort_ID = ValueHandler.GetIntNumberValue(this.ddl_PInfo_PSort_ID.SelectedValue);
            SModel.PInfo_Pwd = this.tb_PInfo_Pwd.Text;
            SModel.PInfo_Id = Id;
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("编码或者名称已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
                else
                    ExtAspNet.Alert.Show("产品信息编辑失败！", MessageBoxIcon.Error);
            }
        }
        protected void tb_OK_Click(object sender, EventArgs e)
        {
            if (tb_Pwd.Text.Trim() == LinkService.GetCurrentUser().SUser_Pwd)
            { 
                tb_PInfo_Cost.Hidden = false;
                win_Content.Hidden = true;
                tb_Pwd.Text = "";
          
            }

        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
            tb_PInfo_Cost.Hidden = true; 
            bnMore.OnClientClick = win_Content.GetShowReference();
        }
        private void BindDropDownList()
        {
          
            ProductSortOp SortManager = new ProductSortOp();
            dtList = SortManager.GetList("");
            dtList.Columns.Add(new DataColumn("ilevel", typeof(System.Int32)));
            DataTable dtRes=dtList.Clone();
            ConstructLevel(dtRes, 0,0);
            ddl_PInfo_PSort_ID.DataSource = dtRes;
            ddl_PInfo_PSort_ID.DataBind();
           // ddl_PInfo_PSort_ID.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));            
        }
        private void ConstructLevel(DataTable dtRes, int id, int ilevel)
        {
            DataView dv = new DataView(dtList);
            dv.RowFilter = "PSort_PSort_ID="+id.ToString(); 
            if(dv.Count>0)  ilevel++;
            for (int i = 0; i < dv.Count; i++)
            {
                dv[i]["ilevel"] = ilevel;
                dtRes.ImportRow(dv[i].Row);
               
                ConstructLevel(dtRes, ValueHandler.GetIntNumberValue(dv[i]["PSort_ID"]), ilevel);
            }
        }
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.PInfo_Id = Id;
                SModel = Manager.GetModel(SModel) as ProductInfo;
                this.tb_PInfo_CH.Text = SModel.PInfo_CH;
                this.tb_PInfo_Code.Text = SModel.PInfo_Code;
                this.tb_PInfo_Cost.Text = SModel.PInfo_Cost.ToString();
                this.tb_PInfo_Memo.Text = SModel.PInfo_Memo;
                this.tb_PInfo_Name.Text = SModel.PInfo_Name;
                this.tb_PInfo_Number.Text = SModel.PInfo_Number;
                this.tb_PInfo_Price.Text = SModel.PInfo_Price.ToString();
                this.tb_PInfo_Pwd.Text = SModel.PInfo_Pwd;
                this.ddl_PInfo_PSort_ID.SelectedValue = SModel.PInfo_PSort_ID.ToString();
            }
            else
            {
                this.tb_PInfo_Code.Text = Coder.CreateCoder(EMenuList.产品管理);
            }
        }

        #endregion

        #region Property
        ProductInfoOp Manager = new ProductInfoOp();
        new Sale_Model.ProductInfo SModel = new Sale_Model.ProductInfo();
        #endregion
    }
}
